// 设置分享的平台渠道: 1=H5,2=微信公众号网页,3=微信小程序,4=App,...按需扩展
const platformMap = ['H5', 'WechatOfficialAccount', 'WechatMiniProgram', 'App'];
import {
	getToken
} from '@/util/auth';
// 设置分享方式: 1=直接转发,2=海报,3=复制链接,...按需扩展
const fromMap = ['forward', 'poster', 'link'];
import {
	getShopShareAdd
} from '../apis/index.js';

import store from '@/store/index.js'

const shartInfo = (scene = {
		title: '', // 自定义分享标题
		desc: '', // 自定义描述
		image: '', // 自定义分享图片
		params: {}, // 自定义分享参数
	},
	poster = {
		// 自定义海报数据
		type: 'user',
	}) => {

	let shareInfo = {
		title: '', // 分享标题
		desc: '', // 描述
		image: '', // 分享图片
		path: '', // 分享页面+参数
		link: '', // 分享Url+参数
		query: '', // 分享参数
		poster, // 海报所需数据
	};

	// const shareConfig = store.state.info.platform.share
	console.log(store.state.info.platform,'store.state.info.platformstore.state.info.platform')
	const shareConfig = store.state.info.platform.share
	const query = buildSpmQuery(scene.params);

	shareInfo.query = query;
	// 配置分享链接地址

	shareInfo.link = buildSpmLink(query,shareConfig.linkAddress);
	// shareInfo.link = buildSpmLink(query, shareConfig);

	return shareInfo

}
// 构造spm分享参数
const buildSpmQuery = (params) => {

	const user = store.state.userInfos

	let shareId = '0'; // 设置分享者用户ID
	if (typeof params.shareId === 'undefined') {
		shareId = user.id;
	}
	let page = '1'; // 页面类型: 1=首页(默认),2=商品,3=拼团商品,4=秒杀商品,5=邀请参团...按需扩展
	if (typeof params.page !== 'undefined') {
		page = params.page;
	}
	let query = '0'; // 设置页面ID: 如商品ID、拼团ID等
	if (typeof params.query !== 'undefined') {
		query = params.query;
	}

	// #ifdef APP
	platform = 'App'
	// #endif
	// #ifdef H5
	platform = 'H5'
	// #endif
	// #ifdef MP-WEIXIN
	platform = 'WechatMiniProgram'
	// #endif

	let platform = platformMap.indexOf(platform) + 1;
	let from = '1';
	if (typeof params.from !== 'undefined') {
		from = platformMap.indexOf(params.from) + 1;
	}
	//spmParams = ...  可按需扩展
	return `spm=${shareId}.${page}.${query}.${platform}.${from}`;
};



const decryptSpm = (spm) => {
	let shareParamsArray = spm.split('.');
	let shareParams = {
		spm,
		shareId: 0,
		page: '',
		query: {},
		platform: '',
		from: '',
	};

	let query;
	shareParams.shareId = shareParamsArray[0];
	switch (shareParamsArray[1]) {
		case '1':
			// 默认首页不跳转
			shareParams.page = '/pages/index/index';
			break;
		case '2':
			// 普通商品
			shareParams.page = '/templatePage/productDetail/productDetail';
			shareParams.query = {
				index: shareParamsArray[2],
			};
			break;
		case '3':
			// 拼团商品
			shareParams.page = '/templatePage/productDetail/productDetail';
			query = shareParamsArray[2].split(',');
			shareParams.query = {
				id: query[0],
				activity_id: query[1],
			};
			break;
		case '4':
			// 秒杀商品
			shareParams.page = '/templatePage/productDetail/productDetail';
			query = shareParamsArray[2].split(',');
			shareParams.query = {
				id: query[0],
				activity_id: query[1],
			};
			break;
		case '5':
			// 参与拼团
			shareParams.page = '/templatePage/productDetail/productDetail';
			shareParams.query = {
				id: shareParamsArray[2],
			};
			break;
	}
	shareParams.platform = platformMap[shareParamsArray[3] - 1];
	shareParams.from = 'link';



	if (shareParams.shareId != 0) {
		// 已登录 立即添加分享记录
		let isLogin = getToken()
		if (isLogin) {
			getShopShareAdd(shareParams)
		} else {
			uni.setStorageSync('shareLog', shareParams);
		}
	}
	console.log(shareParams)
	return shareParams;
};



// 构造分享链接
const buildSpmLink = (query, linkAddress = '') => {
	return `${linkAddress}?${query}`;
};
export {
	shartInfo,
	decryptSpm
}